package com.jingdianjichi.auth.application.controller;

import com.jingdianjichi.auth.application.converter.AuthPermissionDTOConverter;
import com.jingdianjichi.auth.application.converter.AuthRoleDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthPermissionDTO;
import com.jingdianjichi.auth.application.dto.AuthRoleDTO;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthPermissionBO;
import com.jingdianjichi.auth.domain.entity.AuthRoleBO;
import com.jingdianjichi.auth.domain.service.AuthPermissionDomainService;
import com.jingdianjichi.auth.domain.service.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.target.LazyInitTargetSource;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/*
* 角色Controller
* */
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /*
    * 新增权限
    * */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("PermissionController.add.authPermissionDTO:{}",authPermissionDTO);
            }
            Objects.requireNonNull(authPermissionDTO.getName(),"权限名字不能为空");
            Objects.requireNonNull(authPermissionDTO.getParentId(),"父级id不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.Instance.DTOConverterBO(authPermissionDTO);
            Boolean result = authPermissionDomainService.add(authPermissionBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("PermissionController.add.authPermissionDTO:{}",e.getMessage(),e);
            return Result.fail("新增权限失败");
        }
    }

    /*
    * 修改权限
    * */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("PermissionController.update.authPermissionDTO:{}",authPermissionDTO);
            }
            Objects.requireNonNull(authPermissionDTO.getId(),"权限的Id不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.Instance.DTOConverterBO(authPermissionDTO);
            Boolean result = authPermissionDomainService.update(authPermissionBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("PermissionController.update.authPermissionDTO:{}",e.getMessage(),e);
            return Result.fail("修改权限失败");
        }
    }

    /*
    * 删除权限
    * */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("PermissionController.delete.authPermissionDTO:{}",authPermissionDTO);
            }
            Objects.requireNonNull(authPermissionDTO.getId(),"权限的Id不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.Instance.DTOConverterBO(authPermissionDTO);
            Boolean result = authPermissionDomainService.delete(authPermissionBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("PermissionController.delete.authPermissionDTO:{}",e.getMessage(),e);
            return Result.fail("删除权限失败");
        }
    }

    /*
    * 查询权限
    * */
    @PostMapping("/getPermission")
    public Result<List<String>> getPermission(@RequestParam String userName){
        if(log.isInfoEnabled()){
            log.info("PermissionController.getPermission.userName:{}",userName);
        }

        Objects.requireNonNull(userName,"用户名不能为空");
        return Result.ok(authPermissionDomainService.getPermission(userName));
    }
}
